define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

	var form = $(".row");
	if ($(".datetimepicker", form).size() > 0) {
	     require(['bootstrap-datetimepicker'], function () {
	     	var options = {
                format: 'YYYY-MM-DD HH:mm:ss',   //
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-history',
                    clear: 'fa fa-trash',
                    close: 'fa fa-remove'
                },
                showTodayButton: true,
                showClose: true
            };
            $('.datetimepicker', form).parent().css('position', 'relative');
            $('.datetimepicker', form).datetimepicker(options);
		});
	}
	
    var Controller = {
        index: function () {
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: [__('Setup Num'), __('Login Num')]
                },
                toolbox: {
                    show: false,
                    feature: {
                        magicType: {show: true, type: ['stack', 'tiled']},
                        saveAsImage: {show: true}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Orderdata.column
                },
                yAxis: {},
                grid: [{
                    left: '5%',
                    top: 'top',
                    right: '5%',
                    bottom: 30
                }],
                series: [
	            	{
	                    name: __('Setup Num'),
	                    type: 'bar',
	                    barWidth: 40,
	                    smooth: true,
	                    areaStyle: {
	                        normal: {}
	                    },
	                    lineStyle: {
	                        normal: {
	                            width: 1.5
	                        }
	                    },
	                    data: Orderdata.paydata
	                },
	                {
                        name: __('Login Num'),
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: Orderdata.createdata
	                }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);

            // 动态添加数据，可以通过Ajax获取数据然后填充
//            setInterval(fill_data(), 2000000);
            
            $(window).resize(function () {
                myChart.resize();
            });

            // 检查更新
            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });

            //读取信息
            $.ajax({
                url: Config.url + '/../user/comment/get',
                type: 'post',
                dataType: 'json',
                success: function (ret) {
                    $("#news-list").html(Template("discussiontpl", {news: ret}));
                }
            });
            
            $.ajax({
                url: Config.url + '/../user/notice/get',
                type: 'post',
                dataType: 'json',
                success: function (ret) {
                    $("#discussion-list").html(Template("newstpl", {news: ret}));
                }
            });
            
            // 查询
        	$("#find_user").click(function() {
        		if(! check_time()) return ;
        		
        		$.ajax({
        			url: Config.url + '/../dashboard/get',
        			data: {
        				begin: $("input[name=begin_time]").val(),
        				end: $("input[name=end_time]").val()
        			},
        			type: 'post',
        			dataType: 'json',
        			success: function (data){
        				Orderdata.column = data.daylist
        				Orderdata.paydata = data.devicelist
        				Orderdata.createdata = data.loginlist
        				
        				fill_data();
        			}
        		})
        	});
        	
        	// 导出
        	$("#exl_user").click(function() {
        		if(! check_time()) return ;
        		
        		$.ajax({
        			url: Config.url + '/../dashboard/excel',
        			data: {
        				begin: $("input[name=begin_time]").val(),
        				end: $("input[name=end_time]").val()
        			},
        			type: 'post',
        			dataType: 'json',
        			success: function (data){
        				Toastr.success("数据导出成功!");
        				
        				var $a = $("<a>");
                        $a.attr("href", data.file);
                        $a.attr("download", data.filename);
                        $("body").append($a);
                        $a[0].click();
                        $a.remove();
        			}
        		})
        	});
        	
        	function check_time(){
        		let begin = $("input[name=begin_time]").val();
        		let end = $("input[name=end_time]").val();
        		if(!begin){
        			Toastr.error("请选择开始时间!");
        			return false;
        		}
        		
        		if(!end){
        			Toastr.error("请选择结束时间！");
        			return false;
        		}
        		
        		let startTime = new Date(Date.parse(begin));
        		let endTime = new Date(Date.parse(end));
        		
        		if(startTime > endTime){
        			Toastr.error("开始时间不能大于结束时间！");
        			return false;
        		}
        		
        		return true;
        	}
        	
        	function fill_data() {
        	    myChart.setOption({
        	        xAxis: {
        	            data: Orderdata.column
        	        },
        	        series: [
        	        	{
        	                name: __('Setup Num'),
        	                data: Orderdata.paydata
        	            },
        	            {
        	                name: __('Login Num'),
        	                data: Orderdata.createdata
        	            }
        	        ]
        	    });
        	    if ($("#echart").width() != $("#echart canvas").width() && $("#echart canvas").width() < $("#echart").width()) {
        	        myChart.resize();
        	    }
        	}
        }
    };

    return Controller;
});